/**
 * Copyright (C) 2010 David Simonek <dafesimonek@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.dafe.partyband.importer.impl;

import eu.medsea.mimeutil.MimeType;
import eu.medsea.mimeutil.MimeUtil;
import eu.medsea.mimeutil.detector.ExtensionMimeDetector;
import eu.medsea.mimeutil.detector.MagicMimeMimeDetector;
import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.dafe.partyband.importer.api.IMimeTypeDetector;
import org.openide.util.NbBundle;

/**
 *
 * @author dafe
 */
public class MimeUtilDetector implements IMimeTypeDetector {

    private static final Map<String, ContentType> MIME_2_TYPE = new HashMap<String, ContentType>() {{
        put("application/vnd.oasis.opendocument.text", ContentType.TEXT_WITH_CHORDS);
        put("application/x-vnd.oasis.opendocument.text", ContentType.TEXT_WITH_CHORDS);
        put("audio/x-gtp", ContentType.TABLATURES_NOTES);
        put("audio/midi", ContentType.AUDIO);
        put("audio/x-mpeg", ContentType.AUDIO);
        put("application/msword", ContentType.TEXT_WITH_CHORDS);
        put("audio/x-ptb", ContentType.TABLATURES_NOTES);
        put("text/rtf", ContentType.TEXT_WITH_CHORDS);
        put("text/x-tab", ContentType.TABLATURES_NOTES);
        put("audio/x-tuxguitar", ContentType.TABLATURES_NOTES);
    }};

    private static final Map<String, String> MIME_2_DISPLAY = new HashMap<String, String>() {{
        put("application/vnd.oasis.opendocument.text", "OpenOffice");
        put("application/x-vnd.oasis.opendocument.text", "OpenOffice");
        put("audio/x-gtp", "GuitarPro");
        put("audio/midi", "Midi");
        put("audio/x-mpeg", "Mp3");
        put("application/msword", "MsWord");
        put("audio/x-ptb", "PowerTab");
        put("text/rtf", "RichText");
        put("text/x-tab", "Tablature");
        put("audio/x-tuxguitar", "TuxGuitar");
    }};

    static {
        MimeUtil.registerMimeDetector(ExtensionMimeDetector.class.getName());
        MimeUtil.registerMimeDetector(MagicMimeMimeDetector.class.getName());
    }

    public String getMimeType(File file) {
        Collection mts = MimeUtil.getMimeTypes(file);
        MimeType mt = MimeUtil.getMostSpecificMimeType(mts);
        // #14: sometimes detector returns app/zip for odt as most specific,
        // so correct manually
        String result = mt.toString();
        if (mts.size() == 2 && mt.getSubType().contains("zip")) {
            for (MimeType curMt : (Collection<MimeType>)mts) {
                if (curMt.getSubType().contains("opendocument")) {
                    result = curMt.toString();
                    break;
                }
            }
        }

        return result;
    }

    public String getMimeDisplayName(String mimeType) {
        String mimeDisplay = MIME_2_DISPLAY.get(mimeType);
        if (mimeDisplay != null) {
            return NbBundle.getMessage(MimeUtilDetector.class, mimeDisplay);
        }
        return MimeUtil.getMediaType(mimeType);
    }

    public ContentType getContentType(String mimeType) {
        ContentType mediaType = MIME_2_TYPE.get(mimeType);
        if (mediaType == null) {
            String detectedMedia = MimeUtil.getMediaType(mimeType);
            if ("text".equals(detectedMedia)) {
                mediaType = ContentType.TEXT_WITH_CHORDS;
            } else if ("audio".equals(detectedMedia)) {
                mediaType = ContentType.AUDIO;
            } else if ("video".equals(detectedMedia)) {
                mediaType = ContentType.VIDEO;
            }
        }
        return mediaType != null ? mediaType : ContentType.UNKNOWN;
    }

}
